// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;


using DataModel;


namespace WebLib
{
    public class WebSite
    {


        public void WriteEmbeddedContent(DirectoryInfo dir, string name)
        {
            Assembly me = Assembly.GetExecutingAssembly();
            using (Stream blah = me.GetManifestResourceStream("WebLib." + name))
            {
                if (blah != null)
                {
                    using (StreamReader reader = new StreamReader(blah))
                    {
                        FileInfo outputInfo = new FileInfo(Path.Combine(dir.FullName, name));

                        using (TextWriter writer = outputInfo.CreateText())
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                writer.WriteLine(line);
                            }

                            writer.Close();
                        }

                        reader.Close();
                    }
                }
            }

        }



        public void Generate(DirectoryInfo dir, FamilyInfo info, WebSiteSettings settings, WebSiteResults webSiteResults)
        {
            FileNameBuilder namer = new FileNameBuilder(dir);

            // TODO - Doug - 8/27/2006 - use the style sheet specified in the settings, if any
            this.WriteEmbeddedContent(dir, "style.css");

            string destFile = Path.Combine(dir.FullName, "tabber.js");
            string sourcefile = AppDomain.CurrentDomain.BaseDirectory +"..\\..\\..\\WebLib\\tabber.js";
            File.Copy(sourcefile, destFile, true);
            HomePage.Generate(info, namer);
            IndividualIndexPage.Generate(info, namer);
            SourceIndexPage.Generate(info, namer);

            foreach (FamilyIndividual indi in info.Individuals.Values)
            {
                IndividualPage.Generate(indi, namer, info);
            }
            foreach (FamilySource source in info.Sources.Values)
            {
                SourcePage.Generate(source, namer, info);
            }
        }

    }
}
